<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size:18px ">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加品牌</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider style="margin-bottom: 50px"></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="拼音" prop="pinyin">
        <el-input v-model="ruleForm.pinyin"></el-input>
      </el-form-item>
      <el-form-item label="LOGO" prop="logo">
        <el-input v-model="ruleForm.logo"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="关键词列表" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            :active-value = "1"
            :inactive-value = "0"
            active-color="#13ce66"
            inactive-color="#ccc">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">退出</el-button>
      </el-form-item>
    </el-form>


  </div>
</template>

<script>
export default {
  name: "AlbumAddNewView",
  data() {
    return {
      ruleForm: {
        name: '',
        pinyin:'',
        logo:'',
        description: '',
        keywords:'',
        sort: '',
        enable:1
      },
      rules: {
        /*trigger表示检查什么情况会触发，blur表示当控件丢失焦点时触发检查*/
        name: [
          {required: true, message: '请输入品牌名称', trigger: 'blur'},
          {min: 2, max: 15, message: '品牌名须在 2 到 15 个字符', trigger: 'blur'}
        ],
        pinyin: [
          {required: true, message: '请输入品牌拼音', trigger: 'blur'},
          {min: 5, max: 100, message: '请确保拼音输入正确', trigger: 'blur'}
        ],
        logo: [
          {required: true, message: '请输入品牌logo的url', trigger: 'blur'},
          { max: 255, message: 'url地址请少于255个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入品牌简介', trigger: 'blur'},
          {min: 2, max: 255, message: '请输入大于五个字符的品牌描述', trigger: 'blur'}
        ],
        keywords: [
          {required: true, message: '请输入品牌的关键字描述(使用,分割)', trigger: 'blur'},
          {min: 0, max: 125, message: '关键字必须在125个字符以内', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入品牌排序序号', trigger: 'blur'},
          {pattern: /^([0-9]{1}|[1-9]{1}[0-9]{1})$/, message: '排序序号须在0到99之间', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:15722/brands/add-new";
          let formData =this.qs.stringify(this.ruleForm);
          /*let formData = 'name=' + this.ruleForm.name
              +'&description=' + this.ruleForm.description
              + '&sort=' + this.ruleForm.sort;*/
          console.log('URL: '+url);
          console.log('参数: '+formData);
          /*在vue cli框架中不能携程 function(response)格式 只能携程then((response)=>{ 方法体 })*/
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) =>{
            /*在整个response中是包含了很多很多的一些列的信息*/
            /*response的data属性存放的就是服务端响应的具体信息*/
            let jsonResult = response.data;
            console.log(jsonResult.state);
            if (jsonResult.state == 20000){
              this.$message({
                message: '添加品牌成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$alert(jsonResult.message, '操作失败', {
                confirmButtonText: '确定',
                callback: action => { //callback当点击关闭时触发，暂时无需求
                }
              });
            }
            /*console.log(response.data);*/
          }) ;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>